/**
 * Created by Administrator on 2017/1/4.
 */
import java.*;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import jdk.nashorn.internal.parser.JSONParser;
import org.json.JSONArray;
import org.json.JSONObject;
import org.jsoup.*;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.jsoup.select.Selector;


public class analysisHtml {
    public static ArrayList<String> getcategoryURL() throws Exception {
        String html = http.get("http://www.bilibili.com/");
        Document doc = Jsoup.parse(html);

        Elements content1 = doc.getElementsByClass("m-i");

        ArrayList<String> urls = new ArrayList<String>();
        for (Element ele : content1) {
            Elements content2 = ele.getElementsByClass("i-link");
            for (Element ele1 : content2) {
                urls.add(ele1.attr("href"));
            }

        }
        urls.remove("//www.bilibili.com");
        urls.remove("//www.bilibili.com/square");
        urls.remove("//live.bilibili.com");
        for (int i = 0; i < urls.size(); i++) {
            String a = "http:" + urls.get(i);
            urls.remove(i);
            urls.add(i, a);
        }
        return urls;
    }

    public static int getCategorynumber() throws Exception {
        ArrayList<String> urls = getcategoryURL();
        ArrayList<String> categoryurls = new ArrayList<String>();
        ArrayList<String> AVurls = new ArrayList<String>();
        for (String url : urls) {
            String html = http.get(url);
            Document doc = Jsoup.parse(html);
            Elements content1 = doc.getElementsByClass("b-head-t");
            for (Element ele : content1) {
                Elements contont2 = ele.getElementsByTag("a");
                for (Element ele1 : contont2) {
                    String out = ele1.attr("href");
                    if (!out.contains("http")) {
                        out = "http://www.bilibili.com" + out;
                    }
                    categoryurls.add(out);
                }
            }
        }
            return categoryurls.size();
    }
      /*public static ArrayList<String[]> getAVinformations()throws  Exception{
          ArrayList<String> urls=getAVurls();
          ArrayList<String[]> informaions=new ArrayList<String[]>();
          for(String url:urls){
              String html=http.get(url);
              Document doc=Jsoup.parse(html);
              Element dianji=doc.getElementById("dianji");
              String dianjinumber=dianji.text();
              Elements users=doc.getElementsByClass("name");
              String user=users.get(0).text();
              Element stow_count=doc.getElementById("stow_count");
              String stow_countnumber=stow_count.text();
              String[] information=new String[4];
              Pattern p = Pattern.compile("\\d{2,}");//这个2是指连续数字的最少个数
              Matcher m = p.matcher(url);
              String AVnumber=m.replaceAll("").trim();
              information[0]=AVnumber;
              information[1]=dianjinumber;
              information[2]=user;
              information[3]=stow_countnumber;
              informaions.add(information);
          }
        return informaions;
      }*/
      public static  ArrayList<information> getinformaionbycategory(int category)throws  Exception{
          int a=14+category;
          int page;
          ArrayList<information> informations=new ArrayList<information>();
            for(page=1;page<=2000;page++){
              String url="http://api.bilibili.com/archive_rank/getarchiverankbypartion?callback=jQuery&type=jsonp&tid="+a+"&pn"+page;
                String json1=http.get(url);
                if(json1.contains("archives is empty!")){
                    continue;
                }
                else {
                    int c = json1.length();
                    String json = json1.substring(7, c - 2);
                    JSONObject jsonObject = new JSONObject(json);
                    JSONArray archives;
                    if (json.contains("data")) {

                        JSONObject data = jsonObject.getJSONObject("data");
                        archives = data.getJSONArray("archives");
                    } else {
                        archives = jsonObject.getJSONArray("archives");
                    }
                    for (int i = 0; i < archives.length(); i++) {
                        JSONObject jsonItem = archives.getJSONObject(i);
                        String s=jsonItem.getString("aid");
                        boolean d=true;
                        for(information in:informations) {
                            if (in.aid == Integer.parseInt(s)) {
                                d = false;
                            }
                        }
                        if(d==true) {
                            information b = new information();
                            b.aid = Integer.parseInt(s);
                            b.author = jsonItem.getString("author");
                            b.favorites = jsonItem.getInt("favorites");
                            b.category = jsonItem.getString("tname");
                            informations.add(b);
                        }

                    }
                }

          }
              return informations;

      }
    public static void main(String[] args)throws Exception {
        System.out.println("b站视频的类数为" + getCategorynumber());
        int categories = getCategorynumber();
        int category = 1;
        for (category =1; category <=categories; category++) {
            ArrayList<information> informations=getinformaionbycategory(category);
            store.insert(informations,category);
        }
    }

}

